package com.example.administrator.myweiyue.ui.module;


import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.concurrent.TimeUnit;

import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2018-01-23.
 */
@Module
public class HttpModule {
    @Provides
    @Singleton
    public OkHttpClient provideOkHttpClient() {
//
//        // log用拦截器
//        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
//
//        // 开发模式记录整个body，否则只记录基本信息如返回200，http协议版本等
//        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
//
//        // 如果使用到HTTPS，我们需要创建SSLSocketFactory，并设置到client
////        SSLSocketFactory sslSocketFactory = null;
//
//        return new OkHttpClient.Builder()
//                // HeadInterceptor实现了Interceptor，用来往Request Header添加一些业务相关数据，如APP版本，token信息
////                .addInterceptor(new HeadInterceptor())
//                .addInterceptor(logging)
//                // 连接超时时间设置
//                .connectTimeout(10, TimeUnit.SECONDS)
//                // 读取超时时间设置
//                .readTimeout(10, TimeUnit.SECONDS)
//
//                .build();
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(5, TimeUnit.SECONDS)        // 连接超时事件
                .readTimeout(5, TimeUnit.SECONDS)
                // 设置log拦截

                .build();

        return okHttpClient;
    }

    @Provides
    @Singleton
    public Retrofit provideRetrofit(OkHttpClient okHttpClient) {
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").serializeNulls().create();
        Retrofit retrofit = new Retrofit.Builder()
                .client(okHttpClient)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                //所有的请求必须一样的base
                .baseUrl("http:baidu")
                .build();
        return retrofit;

    }
}
